package com.bw.finance.utils.Freemark;

import com.alibaba.fastjson.JSON;
import com.bw.finance.eneity.invest.BiaoInfo;
import com.bw.finance.eneity.locana.EbankBidInfo;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.HashMap;

public class FreemarkUtils {


    public static void FreemarkStart(BiaoInfo goods, String freemarkPath, String freemarkName){
        try {
            //创建模板迎亲配置项
            Configuration configuration = new Configuration(Configuration.getVersion());
            //加载模板的路径 读取模板
            ClassPathResource classPathResource = new ClassPathResource("template");
            //将模板加入到freemark里面
            configuration.setDirectoryForTemplateLoading(classPathResource.getFile());
            //设置free的编码格式
            configuration.setDefaultEncoding("utf-8");
            //获取具体的模板文件 并且生成静态页面
            Template template = configuration.getTemplate(freemarkName);
            HashMap map = new HashMap();
            //将数据转换成JSON字符串添加到map的goods里面
            map.put("good", JSON.toJSONString(goods));
            //通过工具类 模板+数据 生成页面字符串
            String templateIntoString = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
            //转换成流，生成页面 读入流
            InputStream inputStream = IOUtils.toInputStream(templateIntoString, "utf-8");
            //输出流
            FileOutputStream outputStream = new FileOutputStream(freemarkPath + goods.getId() + ".html");
            //FileOutputStream outputStream1 = new FileOutputStream( "D://1711A//SeckillViewC//goods//" + goods.getId() + ".html");
            //产生文件到本地
            AddToClient(goods,freemarkName);
            IOUtils.copy(inputStream,outputStream);
            outputStream.close();
            inputStream.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void AddToClient(BiaoInfo goods, String freemarkName){
        try {
            //创建模板迎亲配置项
            Configuration configuration = new Configuration(Configuration.getVersion());
            //加载模板的路径 读取模板
            ClassPathResource classPathResource = new ClassPathResource("template");
            //将模板加入到freemark里面
            configuration.setDirectoryForTemplateLoading(classPathResource.getFile());
            //设置free的编码格式
            configuration.setDefaultEncoding("utf-8");
            //获取具体的模板文件 并且生成静态页面
            Template template = configuration.getTemplate(freemarkName);
            HashMap map = new HashMap();
            //将数据转换成JSON字符串添加到map的goods里面
            map.put("bid", JSON.toJSONString(goods));
            //通过工具类 模板+数据 生成页面字符串
            String templateIntoString = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
            //转换成流，生成页面 读入流
            InputStream inputStream = IOUtils.toInputStream(templateIntoString, "utf-8");
            //输出流
            FileOutputStream outputStream = new FileOutputStream( "D:\\borrowinfo\\finance\\" + goods.getId() + ".html");
            //产生文件到本地
            IOUtils.copy(inputStream,outputStream);
            outputStream.close();
            inputStream.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void freemarkDelete(EbankBidInfo goods,String freemarkPath){
        try {
            File file = new File(freemarkPath + goods.getId() + ".html");
            file.delete();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
